﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight.Threading;
using Hammock;
using Hammock.Web;
using Newtonsoft.Json;
using _1906Ultras.Model;

namespace _1906Ultras.Helpers
{
    public class TwitterHelper : HelperBase
    {
        public TwitterHelper()
        {
            Authority = "http://api.twitter.com";
            Request.Path = "1/lists/statuses.json";
        }

        //https://api.twitter.com/1/lists/statuses.json?slug=wp1906ultras&owner_screen_name=enloes&per_page=1&page=1&include_entities=true
        public void LoadFeed(Action<ObservableCollection<Tweet>> callback)
        {
            LoadFeed(0,0, callback);
        }
        
        public void LoadFeed(long newestId, long oldestId, Action<ObservableCollection<Tweet>> callback)
        {
            AddParameter("slug", "wp1906ultras");
            AddParameter("owner_screen_name", "enloes");
            AddParameter("include_entities", "false");
            if(newestId > 0) AddParameter("since_id", newestId.ToString());
            if(oldestId > 0) AddParameter("max_id", oldestId.ToString());

            BeginRequest((response) =>
                             {
                                 try
                                 {
                                     var tweets = JsonConvert.DeserializeObject<ObservableCollection<Tweet>>(response);
                                     DispatcherHelper.CheckBeginInvokeOnUI(() =>
                                                                               {
                                                                                   GlobalLoading.Instance.IsLoading =
                                                                                       false;
                                                                                   callback(tweets);
                                                                               });
                                 }
                                 catch (Exception e)
                                 {
                                     ErrorCallbackInternal(e);
                                 }
                             });

        }

    }
}
